package com.reading.core.utils;

import com.reading.common.utils.StringUtil;
import com.reading.core.exception.AloneException;
import org.hibernate.validator.HibernateValidator;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * 校验工具
 *
 * @author guwenqi_1995@163.com
 * @date 2019/7/8 2:28 PM
 **/
public class CheckUtil {

    /**
     * 用户名校验（由字母数字下划线组成且开头必须是字母，不能超过6位）
     *
     * @param userName
     * @return
     */
    public static Boolean validateUserName(String userName) {
        if (StringUtil.isEmpty(userName)) {
            return false;
        }
        Pattern pattern = Pattern.compile("[a-zA-Z]{1}[a-zA-Z0-9_]{1,5}");
        return pattern.matcher(userName).matches();
    }

    /**
     * 密码格式校验（字母、数字点、下划线构成，6到16位）
     *
     * @param passworld
     * @return
     */
    public static Boolean validatePass(String passworld) {
        if (StringUtil.isEmpty(passworld)) {
            return false;
        }
        Pattern pattern = Pattern.compile("[a-zA-Z0-9_.]{6,16}");
        return pattern.matcher(passworld).matches();
    }


    /**
     * 昵称格式校验（汉字、字母和数字构成,1到6位）
     *
     * @param nickName
     * @return
     */
    public static Boolean validateNickName(String nickName) {
        if (StringUtil.isEmpty(nickName)) {
            return false;
        }
        Pattern pattern = Pattern.compile("[[\u4e00-\u9fa5]+[a-zA-Z0-9_]+]{1,6}");
        return pattern.matcher(nickName).matches();
    }

    /**
     * 邮箱格式校验（xxx@xxx.xxx）
     *
     * @param email
     * @return
     */
    public static boolean validateEmail(String email){
        if (StringUtil.isEmpty(email)) {
            return false;
        }
        Pattern pattern = Pattern.compile("[a-zA-Z0-9]+[@]{1}[a-zA-Z0-9]+[.][a-z]+");
        return pattern.matcher(email).matches();
    }

    public static void valid(Object object) throws AloneException {
        String validMessage = "";
        //初始化检查器。
        ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
                .configure()
                .failFast( false )
                .buildValidatorFactory();
        Validator validator = validatorFactory.getValidator();
        //检查object
        Set<ConstraintViolation<Object>> set = validator.validate(object);
        //循环set，获取检查结果
        for(ConstraintViolation<Object> voset : set){
            if (StringUtil.isNotEmpty(voset.getMessage())) {
                validMessage = validMessage + voset.getMessage() +";";
            }
        }
        if(StringUtil.isNotEmpty(validMessage)){
            throw new AloneException(validMessage);
        }
    }

    /**
     * 判断参数中是否存在 null
     *
     * @param objs
     * @throws AloneException
     */
    public static void isNull(Object... objs) throws AloneException {
        for (Object obj : objs) {
            if (StringUtil.isNull(obj)) {
                throw new AloneException();
            }
        }
    }

    /**
     * 判断持久化操作后返回的操作成功数是否为0
     *
     * @param row
     * @throws AloneException
     */
    public static void isOk(Integer row) throws AloneException {
        if (row <= 0) {
            throw new AloneException();
        }
    }

    /**
     * 判断对象是否存在
     *
     * @param count
     * @throws AloneException
     */
    public static void isExist(Integer count) throws AloneException {
        if (count <= 0) {
            throw new AloneException();
        }
    }

    /**
     * 获取文本字数
     *
     * @param text
     * @return
     */
    public static Integer getTextNumber(String text) {
        String nText = text.replaceAll("\\s*", "");
        return nText.length();
    }



    public static void main(String[] args) {

    }

}
